let app = getApp();
import Dialog from '../../../dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    storeName:'',
    uploadUrl:'upload.uploadPic', //上传图片
    fileList:[],
    evaluateType:3,
    type:1, //判断是哪个页面跳转
    comment: '',
    images: [],
    recommendList:[
      {
        
        image:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/recommend.png',
        onImage:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/recommend-on.png',
        name:'推荐',
        type:3,
      },
      {
        image:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/ordinary.png',
        onImage:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/ordinary-on.png',
        name:'一般',
        type:2,
      },
      {
        image:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/not-recommend.png',
        onImage:'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/consumption-coupons/not-recommend-on.png',
        name:'不推荐',
        type:1,
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.orderId??'') !== '') {
      this.setData({
        orderId: parseInt(options.orderId),
        type: parseInt(options.type),
        storeName: options.storeName
      })
    }
  },
  initData() {
    this.setData({
      orderId: null,
      evaluateType:1,
      comment: '',
      images: [],
    })
  },
  onanotherPics(){
    let that = this;
    let ig = '2';
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // var tempFilePaths = res.tempFilePaths
        that.unload({
          tempFilePaths: res.tempFilePaths
        }, ig);
      }
    });
  },
  //多张上传方法
  unload(data, ig) {
    console.log(ig);
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    let that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: urlStr,
      filePath: data.tempFilePaths[i],
      name: 'file',
      formData: null,
      success(resdata) {
        var res = JSON.parse(resdata.data);
        console.log(res.data.img_url);
        if (ig == '1') {
          that.data.fileList.push(res.data.img_url);
          that.setData({
            images : that.data.fileList
          });
        } else if (ig == 2) {
          that.data.fileList.push(res.data.img_url);
          that.setData({
            images : that.data.fileList
          });
        }
        console.log(that.data.fileList);
      },
      fail(e) {
        fail++;
      },
      complete(e) {
        i++;
        if (i == data.tempFilePaths.length) { //当图片传完时，停止调用
        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.unload(data, ig); //递归，回调自己
        }
      }
    });
  },
  evaluateText(event) {
    this.setData({
      comment: event.detail.value
    })
  },
  onRecommend(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      evaluateType : type
    })
  },
  evaluateForm() {
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.comment.comment");
    let json = {
      order_id:this.data.orderId,
      comment_type: this.data.evaluateType,
      comment: this.data.comment,
      images: this.data.images 
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
            Dialog.alert({
              message: res.msg
            }).then(() => {
              // on close
              this.initData();
              let pages = getCurrentPages();
              let beforePage = pages[pages.length - 2];
              this.data.type === 1?beforePage.storeorderDetails():beforePage.storeGoodsList()
              setTimeout((res) => {
                wx.navigateBack({
                  delta: 1
                },1000)
              })
            });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})